package com.stn.zjx.givingtreasure.activity.set;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.makeramen.roundedimageview.RoundedImageView;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.base.BaseActivity;
import com.stn.zjx.givingtreasure.event.NetWorkEvent;
import com.stn.zjx.givingtreasure.service.UserService;
import com.stn.zjx.givingtreasure.table.User;
import com.stn.zjx.givingtreasure.utils.QRCodeUtil;
import com.stn.zjx.givingtreasure.utils.StatusBarUtil;
import com.vondear.rxtools.view.RxQRCode;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShowTransferErWeiActivity extends BaseActivity {
    @BindView(R.id.back_bt)
    RelativeLayout backBt;
    @BindView(R.id.action_title)
    TextView actionTitle;
    @BindView(R.id.iv)
    ImageView iv;
    @BindView(R.id.user_iv)
    RoundedImageView userIv;
    @BindView(R.id.user_nickname)
    TextView userNickname;
    @BindView(R.id.user_sex)
    ImageView userSex;
    @BindView(R.id.user_birthday)
    TextView userBirthday;
    @BindView(R.id.center_iv)
    RoundedImageView centerIv;
    private String value = "1.00";
    Bitmap bitmap;

    @Override
    public void networkStatus(NetWorkEvent netWorkEvent) {

    }

    @Override
    public void initComponent() {
        initUser();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_show_transfer_er_wei);
        ButterKnife.bind(this);
        StatusBarUtil.setImmersiveStatusBar(this, false);
        initComponent();
    }

    @OnClick({R.id.back_bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_bt:
                finish();
                break;

        }
    }

    public void initUser() {
        User onlineUser = UserService.getInstance(this).getOnlineLoginUser();
        if (onlineUser == null) {
            return;
        }
        Glide.with(this)
                .asBitmap()
                .load(onlineUser.getUserAvatar())
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        userIv.setImageBitmap(resource);
                        bitmap = resource;
                        if (!TextUtils.isEmpty(onlineUser.getUserAvatar())) {
                            centerIv.setVisibility(View.VISIBLE);
                            centerIv.setImageBitmap(bitmap);
                        }else{
                            centerIv.setVisibility(View.GONE);
                        }
                    }
                });

        userNickname.setText(onlineUser.getUserNickname());
        switch (onlineUser.getUserSex()){
            case 0:
                   userSex.setVisibility(View.GONE);
                break;
            case 1:
                userSex.setVisibility(View.VISIBLE);
                userSex.setImageResource(R.mipmap.male01);
                break;
             case 2:
                 userSex.setVisibility(View.VISIBLE);
                 userSex.setImageResource(R.mipmap.female01);
            break;
        }

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = (formatter.parse(onlineUser.getUserYear() + "-" + onlineUser.getUserMonth() + "-" + onlineUser.getUserDay()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (date != null) {
            userBirthday.setText(formatter.format(date));
        } else {
            userBirthday.setText("");
        }
        get(onlineUser.getUserId()+"", value);
    }

    public void get(String id, String value) {
        JSONObject data = new JSONObject();
        try {
            data.put("stnId", id);
//            data.put("value", value);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Bitmap bitmap =RxQRCode.creatQRCode(data.toString());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            iv.setImageBitmap(bitmap);
                        }
                    });
                }
            }).start();
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}
